package com.cheneyjoo.servlets;

import com.alibaba.fastjson.JSONObject;
import com.cheneyjoo.controller.ClassStudentsController;
import com.cheneyjoo.controller.StudentController;
import com.cheneyjoo.model.STUDENT;
import com.cheneyjoo.tools.AjaxJsonDataHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;

@WebServlet(name = "DistributionStudentServlet",urlPatterns = "/distributionstudent")
public class DistributionStudentServlet extends HttpServlet {
    private static final long serialVersionUID = 3375423700597993668L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        JSONObject jsonObject = AjaxJsonDataHelper.getJsonObject(request);
        String studentId = jsonObject.getString("studentId");
        String classId = jsonObject.getString("classId");
        PrintWriter out = response.getWriter();
        JSONObject result=new JSONObject();
        try {
            ClassStudentsController csc=new ClassStudentsController();
            boolean b = csc.distributionStudentClass(studentId, Integer.parseInt(classId));
            if (b){
                result.put("error",1);
                result.put("msg","成功");
            }else {
                result.put("error",0);
                result.put("msg","失败");
            }
            out.print(result);
        } catch (Exception e) {
            result.put("error",-1);
            result.put("msg","失败");
            out.print(result);
        }finally {
            out.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
